package com.jiuqi.bi.irp.inforesource.controller.datastructure.datamanage;

import com.jiuqi.bi.irp.core.item.DBDataStructure;
import com.jiuqi.bi.irp.core.item.DBDataStructureExt;
import com.jiuqi.bi.irp.inforesource.manager.IResStructureDataManager;
import io.swagger.annotations.Api;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/irp/bi/structure/dataManage")
@Api("信息资源规划|信息资源|数据结构|数据管理")
public class DataManageController {

    @Autowired
    private IResStructureDataManager IResStructureDataManager;

    @GetMapping("/isDatabaseAndDatastructureSync/{resGuid}")
    public Map<String, Object> isDatabaseAndDatastructureSync(@PathVariable String resGuid) {
        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        boolean isSuccess = true;

        try {
            Boolean isSync = IResStructureDataManager.isDatabaseAndDatastructureSync(resGuid);
            result.put("isSync", isSync);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result;
    }

    @GetMapping("/getOriginalFieldInfos/{resGuid}")
    public Map<String, Object> getOriginalFieldInfos(@PathVariable String resGuid) {
        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        boolean isSuccess = true;

        try {
            List<DBDataStructure> structures = IResStructureDataManager.getAttributesFromDataStructureByGuid(resGuid);
            List<DBDataStructureExt> dbDataStructureExts = structures.stream().map(structureItem -> (DBDataStructureExt) structureItem).collect(Collectors.toList());
            result.put("data", dbDataStructureExts);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result;
    }

    @PostMapping("/validPrimaryKey/{resGuid}")
    public Map<String, Object> validPrimaryKey(@PathVariable String resGuid,@RequestBody Map<String, Object> paramBody) {
        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        boolean isSuccess = true;

        List<Map<String, Object>> primaryKeyFieldList = (List) paramBody.get("primaryKeyFieldArr");
        try {
            boolean checkPass = IResStructureDataManager.validPrimaryKey(resGuid,primaryKeyFieldList);
            result.put("checkPass", checkPass);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result;
    }

    @PostMapping("/addData")
    public String addData(@RequestParam("resGuid") String resGuid, @RequestBody Map<String, Object> paramBody) {

        //返回给前端的数据
        JSONObject result = new JSONObject();
        boolean isSuccess = true;

        List<Map<String, Object>> addDataList = (List) paramBody.get("addDataArr");

        try {
            JSONObject addItem =  IResStructureDataManager.addData(resGuid, addDataList);
            result.put("addItem",addItem);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result.toString();
    }

    @PostMapping("/modifyDataByPK")
    public String modifyDataByPK(@RequestParam("resGuid") String resGuid, @RequestBody Map<String, Object> paramBody) {
        //返回给前端的数据
        JSONObject result = new JSONObject();
        boolean isSuccess = true;

        List<Map<String, Object>> modifyDataByPKList = (List) paramBody.get("modifyDataArr");

        try {
            JSONObject modifyItem = IResStructureDataManager.modifyDataByPK(resGuid, modifyDataByPKList);
            result.put("modifyItem",modifyItem);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result.toString();
    }

    @PostMapping("/modifyDataByAllFields")
    public String modifyDataByAllFields(@RequestParam("resGuid") String resGuid, @RequestBody Map<String, Object> paramBody) {
        //返回给前端的数据
        JSONObject result = new JSONObject();
        boolean isSuccess = true;

        List<Map<String, Object>> oldDataList = (List) paramBody.get("oldDataArr");
        List<Map<String, Object>> newDataList = (List) paramBody.get("curDataArr");

        try {
            JSONObject modifyItem = IResStructureDataManager.modifyDataByAllFields(resGuid, oldDataList, newDataList);
            result.put("modifyItem",modifyItem);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result.toString();
    }

    @PostMapping("/deleteDataByPK")
    public Map<String, Object> deleteData(@RequestParam("resGuid") String resGuid, @RequestBody Map<String, Object> paramBody) {

        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        boolean isSuccess = true;

        List<String> tablePKNameList = (List) paramBody.get("curTablePKNameArr");
        List<List<Object>> deleteParamList = (List) paramBody.get("deleteParamArr");

        try {
            IResStructureDataManager.deleteDataByPK(resGuid, tablePKNameList, deleteParamList);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result;
    }

    @PostMapping("/deleteDataByAllFields")
    public Map<String, Object> deleteData(@RequestParam("resGuid") String resGuid, @RequestBody List<Map<String, Object>> AllFieldsInfo) {
        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        boolean isSuccess = true;

        try {
            IResStructureDataManager.deleteDataByAllFields(resGuid, AllFieldsInfo);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result;
    }

    @GetMapping("/clearAllDataByGuid/{resGuid}")
    public Map<String, Object> clearAllDataByGuid(@PathVariable String resGuid) {
        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        boolean isSuccess = true;

        try {
            IResStructureDataManager.clearAllDataByGuid(resGuid);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result;
    }

    @PostMapping("/queryByFilter/{resGuid}/{currentPage}/{pageSize}")
    public String queryByFilter(@PathVariable("resGuid") String resGuid,
                                @PathVariable("currentPage") String currentPage,
                                @PathVariable("pageSize") String pageSize,
                                @RequestBody List<Map<String, Object>> conditions) {

        return IResStructureDataManager.queryByFilter(resGuid, currentPage, pageSize, conditions).toString();
    }

    @PostMapping("/importDbStructSurfaceData/{resGuid}")
    public Map<String, Object> importDbStructSurfaceData(@PathVariable String resGuid, @RequestPart MultipartFile importFile) {
        //返回给前端的数据
        HashMap<String, Object> result = new HashMap<>();
        boolean isSuccess = true;

        try {
            IResStructureDataManager.importDbStructSurfaceData(resGuid, importFile);
        } catch (Exception e) {
            isSuccess = false;
            result.put("errorInfo", e.getMessage());
        } finally {
            result.put("processSuccess", isSuccess);
        }

        return result;
    }

    @PostMapping("/exportDbStructSurfaceData/{resGuid}")
    public void exportDbStructSurfaceData(@PathVariable String resGuid,
                                                         @RequestBody List<Map<String, Object>> conditions,
                                                         HttpServletResponse response) throws Exception {
            IResStructureDataManager.exportDbStructSurfaceData(resGuid, conditions, response);
    }
}
